
#ifndef _TTG_SERVICES_PUBLISHERS_
#define _TTG_SERVICES_PUBLISHERS_
#include "Common/Definitions.h"

#include <list>
#include <vector>
#include "Common/Errors.h"
#include "Common/Structures.h"
#include "Subscribers.h"
#include "PublisherBase.h"

namespace ttg
{
	namespace services
	{
		//Represents multi-avatar publisher.
		class Publisher
		{
			private:
				std::vector<PublisherInfo *> avatars;
			protected:
				//Adds avatar and returns it ID.
				//Note: avatar will be released by ~Publisher().
				int addAvatar(PublisherInfo *avatar);
				//Calls addAvatar(new PublisherInfo(name));
				int addAvatar(const char *name)
						{ return this->addAvatar(new PublisherInfo(name)); }
				//Returns ID of avatar with given name or -1.
				int getAvatarID(const char *name);
				//Sends event to all subscribers of given avatar.
				void sendEvent(int ID, Event *e);
			public:
				//Releases all infos and unregs all avatars.
				virtual ~Publisher();
			//friend class PublisherBase;
		};

		//Contains publishers for common (unnamed) events.
		//E.g. - onExit.
		class CommonPublisher :public Publisher
		{
			private:
				int onExitID;
				int onWorldUpdateID;
			public:
				CommonPublisher();
				//Sends event about exit.
				//Note: "e" will not be released.
				inline void sendOnExitEvent(OnExitEvent *e)
						{ this->sendEvent(onExitID, e); }
				//Sends event about global world update.
				//Note: "e" will not be released.
				inline void sendOnUpdateEvent(UpdateEvent *e)
						{ this->sendEvent(onWorldUpdateID, e); }
		};
	}
}

#endif
